<?php
namespace expand;

date_default_timezone_set('PRC');
//require_once(__DIR__. '/yst/SOAClient.php');
//require_once(__DIR__. '/yst/RSAUtil.php');
require_once(__DIR__. '/yun/YunBase.php');

Class Yunst{

	private $client;
	private $privateKey;
	private $publicKey;
	private $webUrl;
	private $accountSetNo;
	private $sysid;
	private $ftpPassword;
	private $ftpIp;
	private $ftpPort;
	private $ftpAccount;


	public function __construct()
    {
		$this->ftpAccount='100009000406';
		$this->ftpIp = '139.196.12.148';
		$this->ftpPort = '21';
		$this->ftpPassword ='123456';

//		$serverAddress = "http://116.228.64.55:9092/service/soa";  // 测试接口
//		$serverAddress = "https://yun.allinpay.com/service/soa";
//		$serverAddress = 'http://116.228.64.55:6900/service/soa'; // 2.0测试
		$serverAddress = 'https://fintech.allinpay.com/service/soa'; // 2.0修改
		$this->sysid = "100009000406";
//		$this->sysid = '1902271423530473681'; // 2.测试
		$alias = "100009000406";
		$privatePath = ROOT_PATH."public/static/hry/100009000406_private.pem";
		$publicPath = ROOT_PATH."public/static/hry/100009000406_public.pem";
		$pwd = "942961";
		// $pwd = "593589"; // 测试接口
		$signMethod = "SHA1WithRSA";
		
		$this->webUrl = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME').'/';
//		$this->client = new SOAClient();
		$this->client = new YunBase(); // 2.0修改

		$this->accountSetNo = "200232";

//		$this->accountSetNo='200126'; //2.0修改



//		$this->privateKey = RSAUtil::loadPrivateKey($alias, $privatePath, $pwd);
//		$this->publicKey = RSAUtil::loadPublicKey($alias, $publicPath, $pwd);
		$this->client->setServerAddress($serverAddress);
		$this->client->setSignKey();
//		$this->client->setPublicKey($this->publicKey);
		$this->client->setSysId($this->sysid);
//		$this->client->setSignMethod($signMethod);
	}
	
	//创建会员
	public function createMember($bizUserId) {
		$param["bizUserId"] = $bizUserId;
		$param["memberType"] = "3";
		$param["source"] = "2";
		
		$result = $this->client->request("MemberService", "createMember", $param);
		return $result;
	}


	//实名认证
	public function setRealName($bizUserId,$name,$identityNo) {
		$param["bizUserId"] = $bizUserId;
		$param["name"] = $name;
		$param["identityType"] = "1";
		$param["identityNo"] = $this->rsaEncrypt($identityNo, $this->publicKey, $this->privateKey);
		
		$result = $this->client->request("MemberService", "setRealName", $param);
		return $result;
	}

    //创建企业会员
    public function createQyMember($bizUserId) {
        $param["bizUserId"] = $bizUserId;
        $param["memberType"] = "2";
        $param["source"] = "2";

        $result = $this->client->request("MemberService", "createMember", $param);
        return $result;
    }

    //设置企业信息-demo
	public function setCompany($bizUserId,$data){
		$param["bizUserId"] = $bizUserId;
		$param["backUrl"] = $this->webUrl.'/backcall/setcom.html';
		$param["companyBasicInfo"] = [
										'companyName' => $data['companyName'],
										'legalName' => $data['legalName'],
										'identityType' => 1,
										'legallds' => $data['legallds'],
										'legalPhone' => $data['mobile'],
										'accountNo' => $this->rsaEncrypt($data['accountNo'], $this->publicKey, $this->privateKey),
										'parentBankName' => $data['opening_bank']
									];
		$param["isAuth"] = true;
				
		$result = $this->client->request("MemberService", "setCompanyInfo", $param);
		return $result;
	}
	
	//获取会员信息
	public function getMemberInfo($bizUserId){
		$param["bizUserId"] = $bizUserId;
				
		$result = $this->client->request("MemberService", "getMemberInfo", $param);
		return $result;
	}

	/**
	 * 请求已绑定的银行卡  add by hey
	 */
	public function boundCard($bizUserId,$cardNo){
		$param['bizUserId']=$bizUserId;
		$param['cardNo'] = !empty($cardNo) ? $cardNo : '';
		$result = $this->client->request("MemberService", "queryBankCard", $param);
		return $result;
	}

	/**
	 * 请求平台账户集信息  add by hey
	 */
	public function queryAccount($account){
		$param['accountSetNo']=$account;
		$result = $this->client->request("MerchantService", "queryMerchantBalance", $param);
		return $result;
	}



	/**
	 * 请求会员信息  add by hey
	 */
	public function queryMemberInfo($bizUserId){
		$param['bizUserId']=$bizUserId;
		$result = $this->client->request("MemberService", "getMemberInfo", $param);
		return $result;
	}

	
	//请求绑卡
	public function bindCard($bizUserId,$cardNo,$phone,$name,$identityNo,$info=array()){
		$param["bizUserId"] = $bizUserId;
		$param["cardNo"] = $this->rsaEncrypt($cardNo, $this->publicKey, $this->privateKey);
		$param["phone"] = $phone;
		$param["name"] = $name;
		$param["cardCheck"] = "7";
		$param["identityType"] = "1";
		$param["identityNo"] = $this->rsaEncrypt($identityNo, $this->publicKey, $this->privateKey);

		if(!empty($info)){
			$param['validate'] = $this->rsaEncrypt($info['validate'], $this->publicKey, $this->privateKey);
			$param['cvv2'] = $this->rsaEncrypt($info['cvv2'], $this->publicKey, $this->privateKey);
		}
                $data = $param;
                $data['date'] = date('Y-m-d H:i:s');
		file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'bindBankCard.txt',str_replace("\\", "", json_encode($param,JSON_UNESCAPED_UNICODE)).date("Y-m-d H:i:s", time()).PHP_EOL,FILE_APPEND);
		$result = $this->client->request("MemberService", "applyBindBankCard", $param);
		file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'bindBankCard.txt',str_replace("\\", "", json_encode($param,JSON_UNESCAPED_UNICODE)).date("Y-m-d H:i:s", time()).PHP_EOL,FILE_APPEND);
		return $result;
	}
	//确认绑卡
	public function ActbindCard($bizUserId,$data,$code){
		$param["bizUserId"] = $bizUserId;
		$param["tranceNum"] = $data['tranceNum'];
		$param["transDate"] = $data['transDate'];
		$param["phone"] = $data['bank_mobile'];
		$param["verificationCode"] = $code;
		$data = $param;
        $data['date'] = date('Y-m-d H:i:s');
		file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'bindBankCard.txt',str_replace("\\", "", json_encode($param,JSON_UNESCAPED_UNICODE)).date("Y-m-d H:i:s", time()).PHP_EOL,FILE_APPEND);
		$result = $this->client->request("MemberService", "bindBankCard", $param);
		file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'bindBankCard.txt',str_replace("\\", "", json_encode($result,JSON_UNESCAPED_UNICODE)).date("Y-m-d H:i:s", time()).PHP_EOL,FILE_APPEND);
		return $result;
	}
	//解绑银行卡
	public function unbindCard($bizUserId,$cardNo){
		$param["bizUserId"] = $bizUserId;
		$param["cardNo"] = $this->rsaEncrypt($cardNo, $this->publicKey, $this->privateKey);
		
		$result = $this->client->request("MemberService", "unbindBankCard", $param);
		return $result;
	}
	//锁定会员
	public function lockMember($bizUserId){
		$param["bizUserId"] = $bizUserId;
		
		$result = $this->client->request("MemberService", "lockMember", $param);
		return $result;
	}
	//解锁会员
	public function unlockMember($bizUserId){
		$param["bizUserId"] = $bizUserId;
		
		$result = $this->client->request("MemberService", "unlockMember", $param);
		return $result;
	}
	//发送短信验证码
	public function sendVerifCode($bizUserId,$phone,$type=9){
		$param["bizUserId"] = $bizUserId;
		$param["phone"] = $phone;
		$param["verificationCodeType"] = $type;
		
		$result = $this->client->request("MemberService", "sendVerificationCode", $param);
		return $result;
	}
	//设置安全手机
	public function bindMobile($bizUserId,$phone,$code){
		$param["bizUserId"] = $bizUserId;
		$param["phone"] = $phone;
		$param["verificationCode"] = $code;
		
		$result = $this->client->request("MemberService", "bindPhone", $param);
		return $result;
	}

	//查询订单状态
	public function getOrderInfo($bizUserId,$order_sn){
		$param["bizUserId"] = (string)$bizUserId;
		$param["bizOrderNo"] = (string)$order_sn;

		$result = $this->client->request("OrderService", "getOrderDetail", $param);
		$signedValue = json_decode($result['signedValue'], true);
		return $signedValue;
	}

	//解绑安全手机
	public function unBindMobile($bizUserId,$phone,$code){
		$param["bizUserId"] = (string)$bizUserId;
		$param["phone"] = (string)$phone;
		$param["verificationCode"] = (string)$code;

		$result = $this->client->request("MemberService", "unbindPhone", $param);
		return $result;
	}


	//更改安全手机
	public function bindMobile2($bizUserId,$phone,$phone2,$code){
		$param["bizUserId"] = $bizUserId;
		$param["oldPhone"] = $phone;
		$param["newPhone"] = $phone2;
		$param["newVerificationCode"] = $code;
		
		$result = $this->client->request("MemberService", "changeBindPhone", $param);
		return $result;
	}
	//会员签约
	public function QianYue($bizUserId){
		$param["bizUserId"] = $bizUserId;
//		$param["memberType"] = "3";//2.0取消
		$param["jumpUrl"] = $this->webUrl.'member/card.html';
		$param["backUrl"] = $this->webUrl.'backcall/qianyue.html';
		$param["source"] = "1";
//		$this->client->setServerAddress("http://116.228.64.55:6900/yungateway/member/signContract.html?"); // 2.0 测试
		$this->client->setServerAddress("https://fintech.allinpay.com/yungateway/member/signContract.html?"); // 2.0 生产环境
		file_put_contents(date("Y-m-d H", time()).'hey_test.txt',str_replace("\\", "", json_encode($param,JSON_UNESCAPED_UNICODE)).PHP_EOL,FILE_APPEND);
		$result = $this->client->getUrl("MemberService", "signContract", $param);
		file_put_contents(date("Y-m-d H", time()).'hey_test.txt',$result.PHP_EOL,FILE_APPEND);
		return $result;
	}


	//WAP会员签约
	public function wapQianYue($bizUserId){
		$param["bizUserId"] = $bizUserId;
//		$param["memberType"] = "3"; //2.0取消
		$param["jumpUrl"] = urlencode($this->webUrl.'#/index/person/home');
		$param["backUrl"] = urlencode($this->webUrl.'backcall/qianyue.html');
		$param["source"] = "1";

//		$this->client->setServerAddress("http://116.228.64.55:6900/yungateway/member/signContract.html?"); // 2.0 测试
	    $this->client->setServerAddress("https://fintech.allinpay.com/yungateway/member/signContract.html?"); // 2.0 生产环境

		file_put_contents(date("Y-m-d H", time()).'wap_hey_test.txt',str_replace("\\", "", json_encode($param,JSON_UNESCAPED_UNICODE)).PHP_EOL,FILE_APPEND);
		$result = $this->client->getUrl("MemberService", "signContract", $param);
		return $result;
	}


	//企业会员签约
	public function QiYeQianYue($bizUserId){
		$param["bizUserId"] = $bizUserId;
//		$param["memberType"] = "2"; //2.0取消
		$param["jumpUrl"] = urlencode($this->webUrl.'#/index/company/person?refresh=true');
		$param["backUrl"] = urlencode($this->webUrl.'firm.php/backcall/QyQianYue.html');
		$param["source"] = "1";

//		$this->client->setServerAddress("http://116.228.64.55:6900/yungateway/member/signContract.html?"); // 2.0 测试
		$this->client->setServerAddress("https://fintech.allinpay.com/yungateway/member/signContract.html?"); // 2.0 生产环境

		file_put_contents(date("Y-m-d H", time()).'hey_test.txt',str_replace("\\", "", json_encode($param,JSON_UNESCAPED_UNICODE)).PHP_EOL,FILE_APPEND);
		$result = $this->client->getUrl("MemberService", "signContract", $param);
		return $result;
	}

	
	//设置支付密码
	public function setPayPass($bizUserId,$data,$type){
		$param["bizUserId"] = $bizUserId;
		$param["phone"] = $data['mobile'];
		$param["name"] = $data['username'];
		$param["identityType"] = "1";
		$param["identityNo"] = $this->rsaEncrypt($data['certificate'], $this->publicKey, $this->privateKey);
		$param["jumpUrl"] = $this->webUrl.'member/safe.html';
		$param["backUrl"] = $this->webUrl.'backcall/paypwd.html';
		
		if($type == 'set'){
//			$this->client->setServerAddress("http://116.228.64.55:6900/yungateway/pwd/setPayPwd.html?"); // 2.0修改测试
			$this->client->setServerAddress("https://fintech.allinpay.com/yungateway/pwd/setPayPwd.html?"); // 2.0 生产环境

			$result = $this->client->getUrl("MemberPwdService", "setPayPwd", $param);
		}
		if($type == 'rest'){
//			$this->client->setServerAddress("http://116.228.64.55:6900/yungateway/pwd/resetPayPwd.html?"); // 2.0修改测试
			$this->client->setServerAddress("https://fintech.allinpay.com/yungateway/pwd/resetPayPwd.html?"); // 2.0 生产环境
			$result = $this->client->getUrl("MemberPwdService", "resetPayPwd", $param);
		}
		return $result;
	}
	
	//云商通账户充值申请
	public function createRech($bizUserId,$order) {
		$param["bizOrderNo"] = $order['order'];
		$param["bizUserId"] = $bizUserId;
		$param["accountSetNo"] = $this->accountSetNo;
		$param["amount"] = $order['money']*100+$order['fee']*100;
		$param["fee"] = $order['fee']*100;
		$param["validateType"] = 1;
		//$param["frontUrl"] = $this->webUrl .'member/recharge.html';
		$param["backUrl"] = $this->webUrl .'firm.php/backcall/recharge.html';
		file_put_contents(date("Y-m-d H", time()) . 'test_charge.txt', json_encode($param,JSON_UNESCAPED_UNICODE)  . PHP_EOL, FILE_APPEND);

		//$param["ordErexpireDatetime"] = date('Y-m-d H:i:s',time()+24*3600);
		$param["payMethod"] = [
					// 'QUICKPAY_PC' => [
								// 'amount'=>$order['money']*100,
								// 'bankCardNo' => $this->rsaEncrypt($order['bankCardNo'], $this->publicKey, $this->privateKey)
								// ]
					'QUICKPAY_VSP' => [
								'bankCardNo' => $this->rsaEncrypt($order['bankCardNo'], $this->publicKey, $this->privateKey),
								'amount'=>$order['money']*100+$order['fee']*100
								]
					];
		//$param["goodsName"] = "平台充值";
		$param["industryCode"] = "1118";
		$param["industryName"] = "众筹交易";
		$param["source"] = 2;

		$result = $this->client->request("OrderService", "depositApply", $param);
		return $result;
	}
	//企业用户端回调地址
    public function ComcreateRech($bizUserId,$order) {
        $param["bizOrderNo"] = $order['order'];
        $param["bizUserId"] = $bizUserId;
        $param["accountSetNo"] = $this->accountSetNo;
        $param["amount"] = $order['money']*100+$order['fee']*100;
        $param["fee"] = $order['fee']*100;
        $param["validateType"] = 1;
        $param["backUrl"] = $this->webUrl .'firm.php/backcall/comrecharge.html';
        $param["payMethod"] = [
            'QUICKPAY_VSP' => [
                'bankCardNo' => $this->rsaEncrypt($order['bankCardNo'], $this->publicKey, $this->privateKey),
                'amount'=>$order['money']*100+$order['fee']*100
            ]
        ];
        $param["industryCode"] = "1118";
        $param["industryName"] = "众筹交易";
        $param["source"] = 2;

        $result = $this->client->request("OrderService", "depositApply", $param);
        return $result;
    }
    //云商通账户充值申请 - 网关支付
    public function createRech2($bizUserId, $order) {
        $param["bizOrderNo"] = $order['order'];
        $param["bizUserId"] = $bizUserId;
        $param["accountSetNo"] = $this->accountSetNo;
        $param["amount"] = $order['money'] * 100 + $order['fee'] * 100;
        $param["fee"] = $order['fee'] * 100;
        $param["validateType"] = 0;
        $param["frontUrl"] = $this->webUrl . 'member/recharge.html';
        $param["backUrl"] = $this->webUrl . 'backcall/recharge.html';
        $param["payMethod"] = [
            'GATEWAY_VSP' => [
                'gateid' => $order['bankCode'],
                'paytype' => 'B2C',
                'limitPay' => 'no_credit',
                'amount' => $order['money'] * 100 + $order['fee'] * 100
            ]
        ];
        $param["industryCode"] = "1118";
        $param["industryName"] = "众筹交易";
        $param["source"] = 2;

        $result = $this->client->request("OrderService", "depositApply", $param);
        return $result;
    }
    //云商通账户充值申请 - 网关支付(企业端)
    public function createRech3($bizUserId, $order) {
        $param["bizOrderNo"] = $order['order_num'];
        $param["bizUserId"] = $bizUserId;
        $param["accountSetNo"] = $this->accountSetNo;
        $param["amount"] = $order['true_money'] * 100;
        $param["fee"] = $order['fee'] * 100;
        $param["validateType"] = 0;
        $param["frontUrl"] = 'http://team.hrycf.com/#/index/company/recharge-index';
        $param["backUrl"] = $this->webUrl . 'firm.php/backcall/rechargeReturn.html';
        $param["payMethod"] = [
            'GATEWAY_VSP' => [
                'gateid' => $order['bank_code'],
//                'paytype' => 'B2C,B2B',
				'paytype' => 'B2B',
                'limitPay' => 'no_credit',
                'amount' => $order['true_money'] * 100
            ]
        ];
        $param["industryCode"] = "1118";
        $param["industryName"] = "众筹交易";
        $param["source"] = 2;

        $result = $this->client->request("OrderService", "depositApply", $param);
        return $result;
    }

    //云商通账户提现申请
	public function TiMoney($bizUserId,$order){
		$param["bizOrderNo"] = $order['order'];
		$param["bizUserId"] = $bizUserId;
		$param["accountSetNo"] = $this->accountSetNo;
		$param["amount"] = $order['money']*100;
		$param["fee"] = $order['fee']*100;
		$param["validateType"] = 0;
		if(!empty($order['is_company'])){
			$param["backUrl"] = $this->webUrl .'backcall/companyGetCash.html';
			$param["bankCardPro"] = 1;
		}else{
			$param["backUrl"] = $this->webUrl .'backcall/getcash.html';
			$param["bankCardPro"] = 0;
		}
		// $param['payMethod'] = [
							// 'BALANCE' => [['accountSetNo'=>$this->accountSetNo,'amount'=>$order['money']*100]]
							// ];
		$param["bankCardNo"] = $this->rsaEncrypt($order['bankCardNo'], $this->publicKey, $this->privateKey);
//		$param["withdrawType"] = "T0";
		$param["withdrawType"] = "D0"; //2.0修改
		$param["industryCode"] = "1118";
		$param["industryName"] = "众筹交易";
		$param["source"] = 2;
		file_put_contents('tixian.txt',json_encode($param,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
		$result = $this->client->request("OrderService", "withdrawApply", $param);
		return $result;
	}

    //云商通企业账户提现申请
    public function TiMoney2($bizUserId,$order){
//        $param["bizOrderNo"] = 'aaaa1aaqzz11122335';
		$param["bizOrderNo"] = 'bbbb1aaqzz11122335';
        $param["bizUserId"] = 'hryqy18113112163';
        $param["accountSetNo"] = $this->accountSetNo;
//        $param["amount"] = 440700*100;
		$param["amount"] = 92300*100;
        $param["fee"] = 0*100;
        $param["validateType"] = 0;
        $param["backUrl"] = $this->webUrl .'firm.php/backcall/getcash.html';
        // $param['payMethod'] = [
        // 'BALANCE' => [['accountSetNo'=>$this->accountSetNo,'amount'=>$order['money']*100]]
        // ];
        $param["bankCardNo"] = $this->rsaEncrypt('126636777697', $this->publicKey, $this->privateKey);
        $param["bankCardPro"] = 1;
//        $param["withdrawType"] = "T0";
		$param["withdrawType"] = "D0"; //2.0修改
        $param["industryCode"] = "1118";
        $param["industryName"] = "众筹交易";
        $param["source"] = 2;

        $result = $this->client->request("OrderService", "withdrawApply", $param);
        return $result;
    }
	//查询余额
	public function Balance($bizUserId){
		$param['bizUserId'] = $bizUserId;
		$param["accountSetNo"] = $this->accountSetNo;
		
		$result = $this->client->request("OrderService", "queryBalance", $param);
		return $result;
	}
	
	//消费申请
	public function PayMoney($bizUserId,$order){
		$param['payerId'] = $bizUserId;
		$param['recieverId'] = "#yunBizUserId_B2C#";
		$param['bizOrderNo'] = $order['order'];
		$param["amount"] = $order['money']*100;
		$param["fee"] = 0;
		$param["validateType"] = 1;
		$param["frontUrl"] = $this->webUrl .'member/projectmanage.html';
		$param["backUrl"] = $this->webUrl .'backcall/consume.html';
		$param["payMethod"] = [
					'BALANCE' => [['accountSetNo'=>$this->accountSetNo,'amount'=>$order['money']*100]]
					];
		$param["industryCode"] = "1118";
		$param["industryName"] = "众筹交易";
		$param["source"] = 2;
		$result = $this->client->request("OrderService", "consumeApply", $param);
		return $result;
	}
	//单笔代收
    public function DanBiDaiShou($bizUserId,$companybizUserId,$order) {
        $param['bizOrderNo'] = $order['order_sn'];
        $param['payerId'] = $bizUserId;
        $param['recieverList'][] = ['bizUserId' => $companybizUserId, 'amount' => $order['money'] * 100];  //指定项目方为收款人
        $param['tradeCode'] = "1001";
        $param["amount"] = $order['money'] * 100;
        $param["fee"] = 0;
        $param["validateType"] = 1;
        // $param["frontUrl"] = $this->webUrl .'member/projectmanage.html';
        $param["backUrl"] = $this->webUrl . 'backcall/apply.html';
        //$param["backUrl"] = $_SERVER['HTTP_X_FORWARDED_FOR'].'/hry/';
        $param["payMethod"] = [
            'BALANCE' => [['accountSetNo' => $this->accountSetNo, 'amount' => $order['money'] * 100]]
        ];
        $param["goodsName"] = '项目支持';
        $param["industryCode"] = "1118";
        $param["industryName"] = "众筹交易";
        $param["source"] = 2;
		file_put_contents(date("Y-m-d H", time()).'createorder_2.0',json_encode($param,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
        $result = $this->client->request("OrderService", "agentCollectApply", $param);
		file_put_contents(date("Y-m-d H", time()).'createorder_2.0',json_encode($result,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
        return $result;
    }
    
    //单笔代收企业端
    public function DanBiDaiShouCom($bizUserId, $order, $returns) {
        $param['bizOrderNo'] = $order['order_no'];
        $param['payerId'] = $bizUserId;
        //$param['recieverList'][] = ['bizUserId' => $bizUserId, 'amount' => $order['money'] * 100];
        $param['recieverList'] = $returns;
        $param['tradeCode'] = "1001";
        $param["amount"] = $order['money'] * 100;
        $param["fee"] = 0;
        //$param["validateType"] = 1;
        $param["frontUrl"] = $this->webUrl .'member/projectmanage.html';
        $param["backUrl"] = $this->webUrl . 'firm.php/backcall/payBackReturn.html';
        $param["payMethod"] = [
            'BALANCE' => [['accountSetNo' => $this->accountSetNo, 'amount' => $order['money'] * 100]]
        ];
        $param["goodsName"] = '项目支持';
        $param["industryCode"] = "1118";
        $param["industryName"] = "众筹交易";
        $param["source"] = 2;
        file_put_contents(BASE_PATH.'log/'.date("Y-m-d H", time()).'DanBiDaiShouCom.txt',json_encode($param,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
        $result = $this->client->request("OrderService", "agentCollectApply", $param);
        return $result;
    }

    //平台转账

    public function PingTaiZhuanZhang($bizUserId,$order){
        $param['bizTransferNo'] = $order['order_sn'];
        $param['targetBizUserId'] = $bizUserId;
        $param["amount"] = $order['money']*100;
        $param['sourceAccountSetNo'] = '100001';
        $param['targetAccountSetNo'] = '200232';
        $result = $this->client->request("OrderService", "applicationTransfer", $param);
        return $result;
    }

	//单笔代付
	public function signalAgentPay($bizUserId,$order){
		$param['bizOrderNo'] = $order['order'];
		$param['collectPayList'] = $order['list'];
		$param['bizUserId'] = $bizUserId;
		$param['accountSetNo'] = $this->accountSetNo;
		$param["backUrl"] = $this->webUrl .'backcall/signalpay.html';
		// $param["payToBankCardInfo"] = [
			// 'bankCardNo'	=> $this->rsaEncrypt($order['bankCardNo'], $this->publicKey, $this->privateKey),
			// 'amount'	=> $order['money']*100,
			// 'backUrl'	=> $this->webUrl .'backcall/signalpay.html',
			// 'withdrawType'	=> "T0"
		// ];
		$param["amount"] = $order['money']*100;
		$param["fee"] = $order['fee']*100;
		$param['tradeCode'] = "2001";
		
		$result = $this->client->request("OrderService", "signalAgentPay", $param);
		return $result;
	}

    //企业单笔代付
    public function signalAgentPay2($bizOrderNoa,$bizUserId,$bizOrderNo,$amount){
	    $param['bizOrderNo'] = $bizOrderNoa;
        $param['bizUserId'] = $bizUserId;
        $param["amount"] = $amount*100;
        $param['collectPayList'] = [['bizOrderNo'=>$bizOrderNo,'amount'=>$amount*100]];
        $param["fee"] = 0;
        $param['accountSetNo'] = $this->accountSetNo;
        $param["backUrl"] = $this->webUrl .'backcall/signalpay.html';
//        $param["payToBankCardInfo"] = [
//            'bankCardNo'	=> $this->rsaEncrypt('126636777697', $this->publicKey, $this->privateKey),
//            'amount'	=> 1*100,
//            'backUrl'	=> $this->webUrl .'backcall/signalpay.html',
//            'withdrawType'	=> "T0" ,
//            'bankCardPro' => 1,
//            'unionBank' => '104651064283',
//            'bankName' => '中国银行股份有限公司成都丽都支行',
//            'province'=>'四川',
//            'city' =>'成都',
//        ];
        $param['splitRuleList'] = [[
            'bizUserId' => 'hryqy18113112163',
            'amount' => $amount*100,
            'fee' => 0,

        ]];
        $param['tradeCode'] = "2001";

        $result = $this->client->request("OrderService", "signalAgentPay", $param);
        return $result;
    }

	//批量代付
	public function batchPay($order){
		$param['bizBatchNo'] = $order['order'];
		$param['batchPayList'] = $order['list'];
		$param['tradeCode'] = "2001";
		
		$result = $this->client->request("OrderService", "batchAgentPay", $param);
		return $result;
	}
	
	//冻结金额
	public function freezeMoney($bizUserId,$order){
		$param['bizUserId'] = $bizUserId;
		$param['bizFreezenNo'] = $order['order'];
		$param['accountSetNo'] = $this->accountSetNo;
		$param['amount'] = $order['money']*100;
		
		$result = $this->client->request("OrderService", "freezeMoney", $param);
		return $result;
	}
	
	//解冻金额
	public function unfreezeMoney($bizUserId,$order){
		$param['bizUserId'] = $bizUserId;
		$param['bizFreezenNo'] = $order['order'];
		$param['accountSetNo'] = $this->accountSetNo;
		$param['amount'] = $order['money']*100;
		
		$result = $this->client->request("OrderService", "unfreezeMoney", $param);
		return $result;
	}
	
	//退款申请
	public function backOrderTo($bizUserId,$order){
		$param['bizOrderNo'] = $order['order'];
		// $param['oriBizOrderNo'] = $order['oldOrder'];
		$param['oriBizOrderNo'] = 'NOayjXAmNfU3U61542347516';
		$param['bizUserId'] = $bizUserId;
		//$param['feeAmount'] = 1*100;
		// $param['refundList'][] = [
				// 'accountSetNo' => $this->accountSetNo,
				// 'bizUserId'	=> $bizUserId,
				// 'amount'  => $order['money']*100
		// ];
		$param["backUrl"] = $this->webUrl .'backcall/backorder.html';
		$param["amount"] = $order['money']*100;
		$result = $this->client->request("OrderService", "refund", $param);
		return $result;
	}
	//查询bin
	public function CardBin($cardNo){
		$param['cardNo'] = $this->rsaEncrypt($cardNo, $this->publicKey, $this->privateKey);
		
		$result = $this->client->request("MemberService", "getBankCardBin", $param);
		return $result;
	}
	
	//订单状态
	public function orderStatus($bizUserId,$bizOrderNo){
		$param['bizUserId'] = $bizUserId;
		$param['bizOrderNo'] = $bizOrderNo;

		$result = $this->client->request("OrderService", "getOrderDetail", $param);
		return $result;
	}
	
	//查询账户收支明细
	public function queryDetail($bizUserId){
		$param['bizUserId'] = $bizUserId;
		$param['startPosition'] = 1;
		$param['queryNum'] = 10;
		
		$result = $this->client->request("OrderService", "queryInExpDetail", $param);
		return $result;		
	}
	
	//通联通头寸查询
	public function queryReserve(){
		$param['sysid'] = $this->sysid;
		
		$result = $this->client->request("MerchantService", "queryReserveFundBalance", $param);
		return $result;
	}
	
	//平台对账下载
	public function getCheckFile(){
		$param['date'] = date('Ymd',strtotime('-1 day'));
		$param['fileType'] = 1;
		
		$result = $this->client->request("MerchantService", "getCheckAccountFile", $param);
		return $result;
	}
	//平台头寸调拨充值
	public function pullout($order){
		$param['bizOrderNo']  = $order['bizOrderNo'];
		$param['amount']  = $order['amount']*100;
		$param["backUrl"] = $this->webUrl .'backcall/pullout.html';
		$param['source'] = 2;
		
		$result = $this->client->request("MerchantService", "platformPullout", $param);
		return $result;
	}
	
	//余额-确认支付
	public function balancePay($bizUserId,$order,$code){
		$param["bizUserId"] = $bizUserId;
		$param["bizOrderNo"] = $order;
		$param["verificationCode"] = $code;
                $param["tradeNo"] = '';
		$param["consumerIp"] = $_SERVER["REMOTE_ADDR"];
                $data = $param;
                $data['date'] = date('Y-m-d H:i:s');
                file_put_contents('balancePay.txt',json_encode($data,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
		$result = $this->client->request("OrderService", "pay", $param);
		return $result;
	}
	//充值-确认支付
	public function rechargePay($bizUserId,$order,$code,$tradeNo){
		$param["bizUserId"] = $bizUserId;
		$param["bizOrderNo"] = $order;
		$param["tradeNo"] = $tradeNo;
		$param["verificationCode"] = $code;
		$param["consumerIp"] = $_SERVER["REMOTE_ADDR"];

		$result = $this->client->request("OrderService", "pay", $param);
		file_put_contents(date("Y-m-d H", time()).'recharge_2.0',json_encode($result,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
		return $result;
	}

	//2.0加密
	public function rsaEncrypt($str, $publicKey, $privateKey) {
		$rsaUtil = new YunBase();
		$encryptStr = $rsaUtil->strEncryptWithRsa($str);
		return $encryptStr;
	}

	//2.0解密
	public function rsaDecrypt($str, $publicKey, $privateKey) {
		$rsaUtil = new YunBase();
		$encryptStr = $rsaUtil->strDecryptWithRsa($str);
		return $encryptStr;
	}


//	//加密
//	public function rsaEncrypt($str, $publicKey, $privateKey) {
//		$rsaUtil = new RSAUtil($publicKey, $privateKey);
//		$encryptStr = $rsaUtil->encrypt($str);
//		return $encryptStr;
//	}
//
//	//解密
//	public function rsaDecrypt($str, $publicKey, $privateKey) {
//		$rsaUtil = new RSAUtil($publicKey, $privateKey);
//		$encryptStr = $rsaUtil->decrypt($str);
//		return $encryptStr;
//	}

	//验证
	public function CheckSign($result){
		return $this->client->CheckSign($result);
	}




	/**
	 *企业端单笔代付
	 * @param $bizOrderNoa 商户订单号
	 * @param $bizUserId 代收订单账户id
	 * @param $bizOrderNo 系统订单号
	 * @param $amount 订单金额
	 * @param $companyBizUserId 分账企业账户
	 * @author hey_heyang(635870453@qq.com)
	 * @return  mixed
	 */
	public function signalAgentPayForCompany($bizOrderNoa,$bizUserId,$bizOrderNo,$amount,$companyBizUserId){
		$param['bizOrderNo'] = $bizOrderNoa;
		$param['bizUserId'] = $companyBizUserId;
		$param["amount"] = $amount*100;
		$param['collectPayList'] = [['bizOrderNo'=>$bizOrderNo,'amount'=>$amount*100]];
		$param["fee"] = 0;
		$param['accountSetNo'] = $this->accountSetNo;
		$param["backUrl"] = $this->webUrl .'firm.php/backcall/signalpay.html';
//		$param['splitRuleList'] = [[
//				'bizUserId' => $companyBizUserId,
//				'amount' => $amount*100,
//				'fee' => 0,
//
//		]];
		$param['tradeCode'] = "2001";

		$result = $this->client->request("OrderService", "signalAgentPay", $param);
		return $result;
	}


	/**
	 *企业端设置企业信息
	 * @param $bizUserId 企业用户的业务账户
	 * @param $data 企业用户相关信息
	 * @author hey_heyang(635870453@qq.com)
	 * @return  mixed
	 */
	public function setProjectCompany($bizUserId,$data){
		$param["bizUserId"] = $bizUserId;
		$param["backUrl"] = $this->webUrl.'firm.php/backcall/backproject.html';
		$param["companyBasicInfo"] = [
				'companyName' => $data['company_name'],
				'legalName' => $data['legal_person'],
				'identityType' => 1,
			    'authType'=>2,
				'legalIds' => $this->rsaEncrypt($data['legal_person_id_card'], $this->publicKey, $this->privateKey),
				'legalPhone' => $data['mobile'],
				'accountNo' => $this->rsaEncrypt($data['base_count'], $this->publicKey, $this->privateKey),
				'parentBankName' => $data['base_bank_name'],
			    'uniCredit'=>$data['unicredit'],
			    'bankName'=>$data['bankName'],
				'unionBank'=>$data['unionBank'],
				'province'=>$data['province'],
				'city'=>$data['city'],
		];
		$param["isAuth"] = true;
//		$param["isAuth"] = false;

		$result = $this->client->request("MemberService", "setCompanyInfo", $param);
		return $result;
	}


	/**
	 *设置企业端信息通过ftp的方式向通联传递数据
	 * @param $bizUserId 企业用户的业务账户
	 * @param $data 企业用户公司注册相关信息
	 * @author hey_heyang(635870453@qq.com)
	 * @return  true|false
	 */
	public function ftpTonglianProjectInfo($bizUserId,$data){
		//首先处理$data里面的图片路径
		$file_lj = str_replace("\\","/",ROOT_PATH.'public');
		$upload_file_src = $file_lj.'/uploads/projectuser/'.$data['id'].'/';
		$business_license=$upload_file_src.$data['business_license'];
		$legal_person_id_front=$upload_file_src.$data['legal_person_id_front'];
		$legal_person_id_back=$upload_file_src.$data['legal_person_id_back'];
		$company_count=$upload_file_src.$data['company_count'];

		$ftp_res['business_license'] = $business_license;
		$ftp_res['legal_person_id_front'] = $legal_person_id_front;
		$ftp_res['legal_person_id_back'] = $legal_person_id_back;
		$ftp_res['company_count'] = $company_count;
		file_put_contents(date("Y-m-d H", time()) . 'ftp_res.txt', serialize($ftp_res) . PHP_EOL, FILE_APPEND);
		if(!file_exists($business_license) || !file_exists($business_license) || !file_exists($business_license) || !file_exists($business_license)){
			return false;
		}
		file_put_contents(date("Y-m-d H", time()) . 'ftp_res.txt', 'ftp_in' . PHP_EOL, FILE_APPEND);



		$ftp_data['business_license.jpg']=$business_license;
		$ftp_data['legal_cerificate_front.jpg']=$legal_person_id_front;
		$ftp_data['legal_cerificate_back.jpg']=$legal_person_id_back;
		$ftp_data['bank_settlement.jpg']=$company_count;

// 进行ftp连接，根据port是否设置，传递的参数会不同
		if(empty($this->ftpPort)){
			$f_conn = ftp_connect($this->ftpIp);
		}else{
			$f_conn = ftp_connect($this->ftpIp, $this->ftpPort);
		}
		if(!$f_conn){
			return false;
		}
// 进行ftp登录，使用给定的ftp登录用户名和密码进行login
		$f_login = ftp_login($f_conn,$this->ftpAccount,$this->ftpPassword);
		if(!$f_login){
			return false;
		}
// 获取当前所在的ftp目录
		$in_dir = ftp_pwd($f_conn);
		if(!$in_dir){
			return false;
		}
//// 获取当前所在ftp目录下包含的目录与文件
//		$exist_dir = ftp_nlist($f_conn, ftp_pwd($f_conn));

// 要求是按照日期在ftp目录下创建文件夹作为文件上传存放目录
		$dir_name = date("Ymd").'/'.$bizUserId; // 例如 20190424/hry18380461109
		$parts = explode('/',$dir_name);
		foreach($parts as $part) {
			if (!@ftp_chdir($f_conn, $part)) {
				ftp_mkdir($f_conn, $part);
				ftp_chmod($f_conn, 0777, $part);
				ftp_chdir($f_conn, $part);
			}
		}
//// 检查ftp目录下是否已存在当前日期的文件夹，如不存在则进行创建
//		if(!in_array($dir_name, $exist_dir)){
//			ftp_mkdir($f_conn, $dir_name);
//		}
//// 切换目录
//		if(!ftp_chdir($f_conn, $dir_name)){
//			return false;
//		}
// 进行文件上传
		foreach($ftp_data as $key=>$value){
			$result = ftp_put($f_conn, $key, $value, FTP_BINARY);
			if($result===true){
				$res= 'YES';
			}else{
				$res= 'NO';
			}
			file_put_contents(date("Y-m-d H", time()) . 'ftp_res.txt', $res.'in_2_'.$value . PHP_EOL, FILE_APPEND);
			if(!$result){
				return false;
			}
		}
        return true;
	}


	/**
	 *企业端单笔兑付
	 * @author hey_heyang(635870453@qq.com)
	 * @return  true|false
	 */
	public function signalDuiFu($bizOrderNoa,$bizUserId,$bizOrderNo,$amount){
		$param['bizOrderNo'] = $bizOrderNoa;
		$param['bizUserId'] = $bizUserId;
		$param["amount"] = $amount*100;
		$param['collectPayList'] = [['bizOrderNo'=>$bizOrderNo,'amount'=>$amount*100]];
		$param["fee"] = 0;
		$param['accountSetNo'] = $this->accountSetNo;
		$param["backUrl"] = $this->webUrl .'backcall/signalpay.html';
//		$param['splitRuleList'] = [[
//				'bizUserId' => 'hryqy18113112163',
//				'amount' => $amount*100,
//				'fee' => 0,
//
//		]];
		$param['tradeCode'] = "2001";

		$result = $this->client->request("OrderService", "signalAgentPay", $param);
		return $result;
	}


	/**
	 *获取银行卡信息
	 * @author hey_heyang(635870453@qq.com)
	 * @return  array()
	 */
	public function getCardInfo($cardNo){
		$param['cardNo'] =  $this->rsaEncrypt($cardNo, $this->publicKey, $this->privateKey);
		$result = $this->client->request("MemberService", "getBankCardBin", $param);
		return $result;
	}

}
?>